package com.iee.file.io;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName fileReader
 * @Description TODO
 * @Author longxiaonan@163.com
 * @Date 2018/7/29 15:49
 */
public class fileReaderTest {
    public static void main(String[] args) {
//        test1();
//        test2();
        test3();
    }

    private static void test3() {
        try {
            // java.io.FileNotFoundException: conf\bb.ini (系统找不到指定的路径。)
//            BufferedReader reader = new BufferedReader(new FileReader("conf/bb.ini"));
            // 加载不到classpath
//            BufferedReader reader = new BufferedReader(new FileReader("classpath:conf/bb.ini"));

            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource resource = resolver.getResource("classpath:conf/lmwy_product2.sql");
            //转换成reader
            InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = null;

            while (true) {
                line = reader.readLine();
                if (line == null) {
                    break;
                }
//                System.out.println(line);
                handle(line);
            }
            list.forEach(a -> {
                try {
                    FileUtils.writeLines(new File("product_model_five.txt"), list);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static String specCode;
    private static List<String> list = Lists.newArrayList();

    private static void handle(String line) {
        boolean flag = StringUtils.startsWithIgnoreCase(line, "CREATE TABLE");
        if(flag){
            specCode = line;
        }
        int i = line.indexOf("`product_model_five` varchar(");
        if(i > -1 && Objects.nonNull(specCode)){
            list.add(specCode + ">>>" + line);
            System.out.println(specCode + ">>>" + line);
            specCode = null;
        }
    }

    private static void test2() {
        try {
            BufferedReader br = Files.newBufferedReader(Paths.get("conf/aa.properties"), Charset.forName("utf8"));
            while(true){
                String line = br.readLine();
                if(line == null){
                    break;
                }
                System.out.println(br.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
